var origCats=false;
var origFontSize=-1;
var origFontColor='';
var origBkStyle='';

function populateConfig()
{
  var cfg=readConfig();
  var children=cfg.documentElement.childNodes;
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='NotesList')
      populateListConfig(children[x]);
    if(children[x].nodeName=='NotesWindow')
      populateNoteConfig(children[x]);
  }
}

function populateListConfig(node)
{
  var children=node.childNodes;
  var cats=true;
  var testv='';
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='SupportsCategories')
      testv=getXMLNodeValue(children[x]);
  }
  
  if(testv==null) testv='';
  testv=testv.toUpperCase();
  if(testv=='F' || testv=='N' || testv=='0' || testv=='.F.' || testv=='.N.' || testv=='FALSE' || testv=='NO')
    cats=false;
  else
    cats=true;

  origCats=cats;    

  //var item=document.getElementById('radgroup');
  //if(cats)  item.selectedIndex=0;
  //else      item.selectedIndex=1;
}

function populateNoteConfig(node)
{
  var children=node.childNodes;
  var fontSize='';
  var fontColor='';
  var bkStyle='';
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='FontSize')
      fontSize=getXMLNodeValue(children[x]);
    if(children[x].nodeName=='FontColour')
      fontColor=getXMLNodeValue(children[x]);
    if(children[x].nodeName=='Style')
      bkStyle=getXMLNodeValue(children[x]);
  }

  if(fontSize==null) fontSize='';
  if(fontColor==null) fontColor='';
  if(bkStyle==null) bkStyle='';
  
  if(fontSize<2 || fontSize>18) fontSize=18;
  
  fontColor=fontColor.toLowerCase();
  if(fontColor!='blue' && fontColor!='black' && fontColor!='green' && fontColor!='red') fontColor='blue';
  
  bkStyle=bkStyle.toLowerCase();
  if(bkStyle!='yellow-lined' && bkStyle!='yellow' && bkStyle!='white-lined' && bkStyle!='white') bkStyle='yellow-lined';    

  origFontSize=fontSize;
  origFontColor=fontColor;
  origBkStyle=bkStyle;

  var item=document.getElementById('fontSize');
  item.selectedIndex=fontSize-2;

  var item2=document.getElementById('fontColour');
  if(fontColor=='blue')         item2.selectedIndex=0;
  else if(fontColor=='black')   item2.selectedIndex=1;
  else if(fontColor=='green')   item2.selectedIndex=2;
  else                          item2.selectedIndex=3;

  var item3=document.getElementById('noteStyle');
  if(bkStyle=='yellow')             item3.selectedIndex=0;
  else if(bkStyle=='yellow-lined')  item3.selectedIndex=1;
  else if(bkStyle=='white')         item3.selectedIndex=2;
  else                              item3.selectedIndex=3;
}

function saveConfig()
{
  //var item1=document.getElementById('radgroup');;
  var item2=document.getElementById('fontSize');
  var item3=document.getElementById('fontColour');
  var item4=document.getElementById('noteStyle');
  var cats=true;
  var changed=false;
  var fontSize=18;
  var fontColor='blue';
  var bkStyle='yello-lined';
  
  //if(item1.selectedIndex==1) cats=false;
  
  fontSize=item2.selectedIndex+2;
  
  switch(item3.selectedIndex)
  {
    case 0:
      fontColor='blue';
      break;
    case 1:
      fontColor='black';
      break;
    case 2:
      fontColor='green';
      break;
    case 3:
      fontColor='red';
      break;
    default:
      fontColor='blue';
  }

  switch(item4.selectedIndex)
  {
    case 0:
      bkStyle='yellow';
      break;
    case 1:
      bkStyle='yellow-lined';
      break;
    case 2:
      bkStyle='white';
      break;
    case 3:
      bkStyle='white-lined';
      break;
    default:
      bkStyle='yellow-lined';
  }
  
  //if(origCats!=cats) changed=true;
  
  if(origFontSize!=fontSize) changed=true;
  if(origFontColor!=fontColor) changed=true;
  if(origBkStyle!=bkStyle) changed=true;

  if(changed)
  {
    modifyExistingConfig(cats,fontSize,fontColor,bkStyle);
  }  
}

function cancelConfig()
{
}


function modifyExistingConfig(_cats,_fontSize,_fontColor,_bkStyle)
{
  var dom = readConfig();
  var catsText;
  if(_cats) catsText=dom.createTextNode("true");
  else      catsText=dom.createTextNode("false");
  
  var fontSizeText=dom.createTextNode(_fontSize);
  var fontColorText=dom.createTextNode(_fontColor);
  var bkStyleText=dom.createTextNode(_bkStyle);

  var e1=dom.createElement("NotesList");
  var e2=dom.createElement("NotesWindow");

  var catsElm=dom.createElement("SupportsCategories");
  catsElm.appendChild(catsText);

  var fontSizeElm=dom.createElement("FontSize");
  fontSizeElm.appendChild(fontSizeText);
  
  var fontColorElm=dom.createElement("FontColour");
  fontColorElm.appendChild(fontColorText);

  var bkStyleElm=dom.createElement("Style");
  bkStyleElm.appendChild(bkStyleText);

  e1.appendChild(catsElm);
  e2.appendChild(fontSizeElm);
  e2.appendChild(fontColorElm);
  e2.appendChild(bkStyleElm);
  
  var children=dom.documentElement.childNodes;
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='NotesList')
      dom.documentElement.replaceChild(e1,children[x]);
    if(children[x].nodeName=='NotesWindow')
      dom.documentElement.replaceChild(e2,children[x]);
  }

  writeConfig(dom);
}
